package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVO;
import com.atguigu.guli.service.edu.entity.vo.CourseListSortVO;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@RestController

@RequestMapping("api/edu/course")
public class ApiCourseController {
    @Autowired
    CourseService courseService;

    @Autowired
    TeacherService teacherService;
    //远程调用更新课程销量
    @PutMapping("updateCourseSale/{courseId}")
    public R updateCourseSale(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        course.setBuyCount(course.getBuyCount()+1);
        courseService.updateById(course);
        return R.ok();
    }
    //远程调用获取课程的信息
    @GetMapping("getCourseDTO/{courseId}")
    public R getCourseDTO(@PathVariable String courseId){
        Course course = courseService.getById(courseId);
        Teacher teacher = teacherService.getById(course.getTeacherId());
        CourseDto courseDto = new CourseDto();
        BeanUtils.copyProperties(course,courseDto);
        courseDto.setTeacherName(teacher.getName());
        return R.ok().data("item",courseDto);
    }

    @GetMapping("getCourseList/{id}")
    public R getCourseList(@PathVariable String id){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getTeacherId,id);
        queryWrapper.select(Course::getId , Course::getTitle, Course::getCover);
        List<Course> list = courseService.list(queryWrapper);
        return R.ok().data("items",list);
    }

    //获取课程列表
    @GetMapping("getCourseListWithSort")
    public R getCourseListWithSort( CourseListSortVO courseListSortVO){
        String subjectId = courseListSortVO.getSubjectId();
        String subjectParentId = courseListSortVO.getSubjectParentId();
        Integer sortId = courseListSortVO.getSort();
        List<Course> courseList =  courseService.getCourseListWithSort(subjectId,subjectParentId,sortId.toString());
       return R.ok().data("items",courseList);
    }

    //获取课程详情
    @GetMapping("getCourseDetailById/{id}")
    public  R getCourseDetailById(@PathVariable String id){
       ApiCourseDetailVO courseDetailVO =  courseService.getCourseDetailById(id);
       return R.ok().data("item",courseDetailVO);
    }

    //获取热门课程信息列表
    @GetMapping("getHotCourse")
    public  R getHotCourse(){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Course::getViewCount);
        queryWrapper.last("limit 0 , 8");
        List<Course> courses = courseService.list(queryWrapper);
        return R.ok().data("items",courses);
    }
}

